package com.example.xymarket.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xymarket.R;
import com.example.xymarket.httpUtil.UserAddressService;
import com.example.xymarket.po.UserAddress;
import com.example.xymarket.common.MyHandler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddressAdapter extends BaseAdapter {

    public List<UserAddress> items;
    Context context;
    int count = 0;
    int layout= 0;
    Handler contextHandler;
    public AddressAdapter(List<UserAddress> items, Context context,Handler handler) {
        super();
        this.items = items;
        this.context = context;
        this.layout=R.layout.item_address;
        count = items.size();
        this.contextHandler=handler;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            UserAddress address = items.get(position);
            AddressAdapter.MyViewHolder viewHolder = new AddressAdapter.MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(layout, null);
            viewHolder.id = address.getId();
            viewHolder.add_name = (TextView) convertView.findViewById(R.id.add_name);
            viewHolder.add_name.setText(address.getName());
            viewHolder.add_phone = (TextView) convertView.findViewById(R.id.add_phone);
            viewHolder.add_phone.setText(address.getPhone());
            viewHolder.add_detail = (TextView) convertView.findViewById(R.id.add_detail);
            viewHolder.add_detail.setText(String.valueOf(address.getAddress()));
            viewHolder.add_update=(TextView) convertView.findViewById(R.id.add_update);
            viewHolder.add_update.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Map<String,String> address_map=new HashMap<>();
                    address_map.put("id", String.valueOf(viewHolder.id));
                    address_map.put("name",viewHolder.add_name.getText().toString());
                    address_map.put("phone",viewHolder.add_phone.getText().toString());
                    address_map.put("address",viewHolder.add_detail.getText().toString());
                    Message message=new Message();
                    message.obj=address_map;
                    message.what=1001;
                    contextHandler.sendMessage(message);
                }
            });
            viewHolder.add_del=(TextView) convertView.findViewById(R.id.add_del);
            viewHolder.add_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MyHandler myHandler=new MyHandler(context,handler);
                    UserAddressService addressService=new UserAddressService(myHandler);
                    addressService.delAddress(viewHolder.id);
                }
            });
            convertView.setTag(viewHolder);
            return convertView;
        }
        catch (Exception e)
        {
            return null;
        }
    }

    @SuppressLint("HandlerLeak")
    final Handler handler = new Handler()
    {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==2) {
                Toast.makeText(context,"删除成功!",Toast.LENGTH_LONG).show();
                contextHandler.sendEmptyMessage(100);
            }
            if(msg.what==-2) {
                Toast.makeText(context,"删除失败!",Toast.LENGTH_LONG).show();
            }
        }
    };


    public final class MyViewHolder {

        int id;
        TextView add_name;
        TextView add_phone;
        TextView add_detail;
        TextView add_update;
        TextView add_del;
    }
}
